package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 创建床位
     * @param bedDto
     */
    @Override
    public void addBed(BedDto bedDto) {
        //对象拷贝
        Bed bed= BeanUtil.toBean(bedDto,Bed.class);
        //创建人
        bed.setCreateBy(UserThreadLocal.getUserId());
        //创建时间
        bed.setCreateTime(LocalDateTime.now());
        //更新时间
        bed.setUpdateTime(LocalDateTime.now());
        bed.setBedStatus(0);

        bedMapper.createBed(bed);
    }

    /**
     * 根据id查询床位
     * @param id
     * @return
     */
    @Override
    public BedVo getByIdBad(Long id) {
        Bed byIdBed = bedMapper.getByIdBed(id);
        BedVo bedVo = new BedVo();
        BeanUtil.copyProperties(byIdBed,bedVo);
        return bedVo;
    }

    /**
     * 根据id删除床位
     * @param id
     */
    @Override
    public void deleteBedById(Long id) {
        bedMapper.deleteBedById(id);
    }


    /**
     * 更新床位
     * @param bedDto
     */
    @Override
    public void updateBed(BedDto bedDto) {
        BedVo bedVo = getByIdBad(bedDto.getId());
        bedVo.setBedNumber(bedDto.getBedNumber());
        bedVo.setSort(bedDto.getSort());
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())){
            bedVo.setBedStatus(bedDto.getBedStatus());
        }
        Bed bed = BeanUtil.toBean(bedVo, Bed.class);
        bed.setCreateBy(UserThreadLocal.getUserId());
        bed.setCreateTime(LocalDateTime.now());
        bedMapper.updateBed(bed);
    }

}

